package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.management.relation.Relation;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrService attrService;

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsByCatalogIdAndSpuId(Long catalogId, Long spuId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        List<SpuItemAttrGroupVo> spuItemAttrGroupVos = this.baseMapper.getAttrGroupWithAttrsBySpuId(catalogId,spuId);
        return spuItemAttrGroupVos;
    }

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId 分类id
     * @return List<AttrGroupWithAttrsVo>
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查出当前分类下的所有属性分组，
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> collect = groupEntities.stream().map(groupEntity -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            //将group分组的数据复制到attrsVo
            BeanUtils.copyProperties(groupEntity, attrGroupWithAttrsVo);
            List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(attrEntities);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        if(catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    //分页条件
                    new Query<AttrGroupEntity>().getPage(params),
                    //查询条件：无
                    new QueryWrapper<AttrGroupEntity>()
            );
            return new PageUtils(page);
        } else {
            String key = (String) params.get("key");
            //select * from pms_attr_group where catelog_id = ? and (attr_group_id = key or attr_group_name like %key%)
            //QueryWrapper：构造查询条件
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
            //查询条件1：数据库字段catelog_id的值与前端传入的参数catelogId相同，即catelog_id=catelogId
            queryWrapper.eq("catelog_id",catelogId);
            //查询条件2：字段 attr_group_id==key 或 字段 attr_group_name like %key%
            if(!StringUtils.isEmpty(key)){
                queryWrapper.and(obj-> obj.eq("attr_group_id",key).or().like("attr_group_name",key));
            }
            //根据分页条件params和查询条件wrapper进行查询
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                //分页条件
                new Query<AttrGroupEntity>().getPage(params),
                //查询条件：无
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

}